function longestConsecutive(nums: number[]): number {
    if (nums.length === 0) return 0;

    let maxLen = 0;
    const numSet = new Set(nums);
    for (const num of numSet) {
        if (!numSet.has(num - 1)) {
            let len = 1;
            let currentNum = num;
            while (numSet.has(currentNum + 1)) {
                len++;
                currentNum++;
            }
            maxLen = Math.max(maxLen, len);
        }
    }

    return maxLen;
}
